package cn.itcast.service.cargo.impl;

import cn.itcast.dao.cargo.ContractDao;
import cn.itcast.dao.cargo.ExtCproductDao;
import cn.itcast.domain.cargo.Contract;
import cn.itcast.domain.cargo.ExtCproduct;
import cn.itcast.domain.cargo.ExtCproductExample;
import cn.itcast.service.cargo.ExtCproductService;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.UUID;

@Service
public class ExtCproductServiceImpl implements ExtCproductService {

    @Autowired
    private ExtCproductDao extCproductDao;
    @Autowired
    private ContractDao contractDao;
    /**
     * 分页查询
     *
     * @param extCproductExample
     * @param pageNum
     * @param pageSize
     */
    @Override
    public PageInfo<ExtCproduct> findByPage(ExtCproductExample extCproductExample, int pageNum, int pageSize) {
        Page<ExtCproduct> page = PageHelper.startPage(pageNum, pageSize);
        List<ExtCproduct> list = extCproductDao.selectByExample(extCproductExample);
        PageInfo<ExtCproduct> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    /**
     * 查询所有
     *
     * @param extCproductExample
     */
    @Override
    public List<ExtCproduct> findAll(ExtCproductExample extCproductExample) {
        List<ExtCproduct> list = extCproductDao.selectByExample(extCproductExample);
        return list;
    }

    /**
     * 根据id查询
     *
     * @param id
     * @return
     */
    @Override
    public ExtCproduct findById(String id) {
        ExtCproduct extCproduct = extCproductDao.selectByPrimaryKey(id);
        return extCproduct;
    }

    /**
     * 新增
     *
     * @param extCproduct
     */
    @Override
    public void save(ExtCproduct extCproduct) {
        //设置id
        extCproduct.setId(UUID.randomUUID().toString());
        //获取附件总金额
        Double amount = 0d;
        if (extCproduct.getCnumber() != null && extCproduct.getPrice() != null){
            amount = extCproduct.getCnumber() * extCproduct.getPrice();
        }
        extCproduct.setAmount(amount);
        //获取contractId
        String contractId = extCproduct.getContractId();
        Contract contract = contractDao.selectByPrimaryKey(contractId);
        Double oldToatalAmount = contract.getTotalAmount();
        contract.setTotalAmount(oldToatalAmount + amount);
        contract.setExtNum(contract.getExtNum() + 1);
        contractDao.updateByPrimaryKeySelective(contract);
        //保存附件
        extCproductDao.insertSelective(extCproduct);

    }

    /**
     * 修改
     *
     * @param extCproduct
     */
    @Override
    public void update(ExtCproduct extCproduct) {
        //获取附件id
        String id = extCproduct.getId();
        ExtCproduct extC = extCproductDao.selectByPrimaryKey(id);
        //获取附件总金额
        Double amount = 0d;
        if (extCproduct.getCnumber() != null && extCproduct.getPrice() != null) {
            amount = extCproduct.getCnumber() * extCproduct.getPrice();
        }
        extCproduct.setAmount(amount);
        Double oldAmount = extC.getAmount();

        String contractId = extCproduct.getContractId();
        Contract contract = contractDao.selectByPrimaryKey(contractId);
        Double oldToatalAmount = contract.getTotalAmount();
        contract.setTotalAmount(oldToatalAmount + amount - oldAmount);
        contractDao.updateByPrimaryKeySelective(contract);

        //修改附件
        extCproductDao.updateByPrimaryKeySelective(extCproduct);
    }

    /**
     * 删除部门
     *
     * @param id
     */
    @Override
    public void delete(String id) {
        //获取附件金额
        ExtCproduct extCproduct = extCproductDao.selectByPrimaryKey(id);
        Double amount = extCproduct.getAmount();
        String contractId = extCproduct.getContractId();
        Contract contract = contractDao.selectByPrimaryKey(contractId);
        //修改订单总金额
        contract.setTotalAmount(contract.getTotalAmount() - amount);
        //修改附件数量
        contract.setExtNum(contract.getExtNum() - 1);
        contractDao.updateByPrimaryKeySelective(contract);
        //删除附件
        extCproductDao.deleteByPrimaryKey(id);
    }
}
